Import-Module FailoverClusters

cls;

$backupPath = "C:\Backup";

Write-Host "This script will restore Databases to mark or to last mark." -foregroundcolor "yellow";
Write-Host "Before continuing make sure:" -foregroundcolor "yellow"
Write-Host "* All SQL cluster resources are active on the current node." -foregroundcolor "yellow";
Write-Host "    (Some resources will be stopped and, when done, restarted by the script)" -foregroundcolor "yellow";
Write-Host "* All BizTalk services on the BizTalk cluster are stopped." -foregroundcolor "yellow";
Write-Host "    (BizTalk Hosts, Rule Engine, IIS/WWW Publishing Service, ENTSSO etc.)" -foregroundcolor "yellow";
Write-Host "Press Enter to continue." -foregroundcolor "yellow";
Read-Host; 

$mark = Read-Host "Specify mark (use the Get-Marks cmdlet to get all marks or blank to use last mark)";

if ($mark.Length -eq 0)
{
	Write-Output "Restoring to last mark...";
	$mark="";
}

Write-Host "Stopping Cluster Resources...";
Stop-ClusterResource "Enterprise Single Sign-On Service"
Stop-ClusterResource "SQL Server Agent (DEFAULT)"
Stop-ClusterResource "SQL Server Agent (BAM)"
Stop-ClusterResource "SQL Server Agent (DTA)"
Stop-ClusterResource "SQL Server Agent (MSGBOX01)"
Write-Host "Done.";

New-RestoreDatabaseFromMark SSODB $backupPath $mark;
New-RestoreDatabaseFromMark BAMPrimaryImport $backupPath $mark;
New-RestoreDatabaseFromMark BizTalkDTADb $backupPath $mark;
New-RestoreDatabaseFromMark BizTalkMsgBoxDb $backupPath $mark;
New-RestoreDatabaseFromMark BizTalkRuleEngineDb $backupPath $mark;
New-RestoreDatabaseFromMark MSHIS_HIP $backupPath $mark;
New-RestoreDatabaseFromMark BizTalkMgmtDb $backupPath $mark;
New-RestoreDatabaseFromMark BAMArchive $backupPath $mark;

Write-Host "Starting Cluster Resources...";
Start-ClusterResource "Enterprise Single Sign-On Service"
Start-ClusterResource "SQL Server Agent (DEFAULT)"
Start-ClusterResource "SQL Server Agent (BAM)"
Start-ClusterResource "SQL Server Agent (DTA)"
Start-ClusterResource "SQL Server Agent (MSGBOX01)"
Write-Host "Done.";

trap [Exception] 
{
	Write-Host $_.Exception.ToString() -foregroundcolor "red";

	Exit;
}

Write-Host "Done restoring all BizTalk databases" -foregroundcolor "yellow";